package commander;

import fileoperation.FileRead;
import fileoperation.FileCopy;
import fileoperation.FileReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
/*
 *• --soft: 仅仅将HEAD指针个指向指定的commit，其他不变

• --mixed: 重置暂存区索引，但是不重置工作区。（更改的文件被保留，但是会被标记为未提交）。这是默认的模式。

•--hard:  同时重置索引与工作区文件，并且此commit之后的所有commit信息将会丢失
 */
//移动头指针HEAD至之前的commit节点
// reset(head,i),i=1时为checkout;reset,reset files
public class reset extends Command{
    private File branch = new File(wrapGit(FileRead.readBranchFromHead(head)));
    //当前分支
    String commitpath;
    {
        try {
            commitpath = FileReader.readCommitTree(FileRead.readCommitFromBranch(branch));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private File commit = new File(wrapObjects(commitpath));
    //首元节点tree da39a3ee5e6b4b0d3255bfef95601890afd80709
 //   private String parent = FileReader.readCommitParent(commit);

    // reset --soft，soft选项时工作区不变动,更改Head所指的Commit
    public void resetSoft() throws Exception{
//        PrintWriter out = new PrintWriter(this.branch);
//        if(this.parent.equals("")){
//            System.out.print("can't reset back");
//            return;
//        }
//        else {
//            out.print(this.parent);
//            System.out.println("HEAD now back to " + this.parent);
//        }
//        out.close();
    }

    // reset --mixed，重置暂存区
    public void resetMixed() throws Exception{
        PrintWriter p = new PrintWriter(this.index);
        p.print("");
        p.close(); // clear index
        System.out.println("Index has been initiated ");
    }

    // reset --hard， hard选项时工作区更新为HEAD所指的commit代表的版本,此时会更改工作区中的文件
    public void resetHard() throws Exception{
        resetMixed();
        System.out.println("commit is " + this.commit.getName());//回溯的版本为
        File tree = new File(wrapObjects(FileRead.readTreeFromCommit(this.commit)));
        System.out.println("tree is " + tree.getName());
        //反序列化
        FileCopy.copyTree(tree,new File(workingDir));//更新工作目录里的文件，
        //此步骤亦可用FileCreation.recoverTree函数
    }


 /*   public static void main(String[] args) throws Exception{
        Options options = new Options();
        CommandLineParser parser = new DefaultParser();
        options.addOption(new Option("s",false,"soft"));
        options.addOption(new Option("m",false,"mixed"));
        options.addOption(new Option("h",false,"hard"));
        CommandLine cmd = parser.parse( options, args);
        if(cmd.hasOption("s")){
            new reset().resetSoft();
        }
        else if (cmd.hasOption("h")){
            new reset().resetHard();
        }
        else{
            new reset().resetMixed();
        }
    }*/
}
